package drds.data_propagate.parse.table_meta_data.dao;

import com.google.common.collect.Maps;
import drds.data_propagate.parse.table_meta_data._do_.MetaDataHistoryDo;

import java.util.HashMap;
import java.util.List;


public class MetaDataDataHistoryDao extends MetaDataBaseDao {

    public Long insert(MetaDataHistoryDo metaDataHistoryDo) {
        return (Long) getSqlMapClientTemplate().insert("meta_history.insert", metaDataHistoryDo);
    }

    public List<MetaDataHistoryDo> findByTimestamp(String taskId,
                                                   Long snapshotTimestamp, Long timestamp) {
        HashMap params = Maps.newHashMapWithExpectedSize(2);
        params.put("taskIdSequense", taskId);
        params.put("snapshotTimestamp", snapshotTimestamp == null ? 0L : snapshotTimestamp);
        params.put("executeTimestamp", timestamp == null ? 0L : timestamp);
        return (List<MetaDataHistoryDo>) getSqlMapClientTemplate().queryForList("meta_history.findByTimestamp", params);
    }

    public Integer deleteByName(String taskId) {
        HashMap params = Maps.newHashMapWithExpectedSize(2);
        params.put("taskIdSequense", taskId);
        return getSqlMapClientTemplate().delete("meta_history.deleteByName", params);
    }

    /**
     * 删除interval秒之前的数据
     */
    public Integer deleteByTimestamp(String taskId, int interval) {
        HashMap params = Maps.newHashMapWithExpectedSize(2);
        long timestamp = System.currentTimeMillis() - interval * 1000;
        params.put("executeTimestamp", timestamp);
        params.put("taskIdSequense", taskId);
        return getSqlMapClientTemplate().delete("meta_history.deleteByTimestamp", params);
    }

    protected void initDao() throws Exception {
        initTable("meta_history");
    }
}
